<?php
namespace app\home\logic;
class CartLogic{
	# 添加到购物车
	public static function getAddCart($goods_id,$goods_spec_id,$number,$is_selected=1){
		# 判断他是否有登录 如果有登录 那就将他加入购物车的数据添加到数据表中，如果没有那就添加到 cookie缓存中
		if(session("?user_info")){
			# 已经是登录
			# 获取用户ID
			$user_id = session("user_info.id");
			# 在进行添加之前判断一下他在数据库里在是否存在数据，如果有那就进行数据量和加就可以了，不用添加
			$where = [
				"user_id"=>$user_id,
				"goods_id" => $goods_id,
				"spec_goods_id" => $goods_spec_id
			];
			$info = \app\common\model\Cart::where($where)->find();
			# 判断是否有查询到一条数据 有为true 没有就是false 
			if($info){
				# 有查询到数据那就进行数据的添加
				$info["number"] += $number;
				$info["is_selected"] = $is_selected;
				$info->save();
			}else{
				# 没有查询到那就进行添加数据 
				\app\common\model\Cart::create(["user_id"=>$user_id,"goods_id" => $goods_id,"spec_goods_id" => $goods_spec_id,"number" => $number,"is_selected" => $is_selected],true);
			}
		}else{
			# 没有登录 存储到 缓存中
			# 先获取到缓存中的数据，看能不能获取到，获取到一条数据那就进行一个修改数量
			$key = $goods_id . "_" . $goods_spec_id;
			# 查询cookie 如果有数据就使用数据 没有那就默认一个
			$data = cookie("cart")?:[];
			if(isset($data[$key])){
				# 有查询到数据那就进行数据的添加
				$data[$key]["number"] += $number;
				$data[$key]["is_selected"] = $is_selected;
			}else{
				# 没有查询到就进行添加一条数据
				$data[$key] = [
					"id" => $key,
					"goods_id" => $goods_id,
					"spec_goods_id" => $goods_spec_id,
					"number" => $number,
					"is_selected" => $is_selected
				];
			}
			# 最后再进行一个总的添加或修改
			cookie("cart",$data,86400*7);
		}
	}
	# 查询到添加的一条数据
	public static function getFindgoods($goods_id,$spec_goods_id){
		if(empty($goods_id)){
			$where["t1.id"] = $goods_id;
		}else{
			$where["t2.id"] = $spec_goods_id;
		}
		$goods = \app\common\model\Goods::alias("t1")->join("spec_goods t2","t1.id=t2.goods_id","left")->field("t1.*,t2.value_ids ,t2.value_names ,t2.cost_price as cost_price2,t2.price ,t2.store_count,t2.store_frozen,t2.id as spec_goods_id")->where($where)->find();
		// dump($goods);
		# 判断是否在查询到一条数据，如果没有那就返回一条空数组
		if(empty($goods)) return [];
		# 价格
		if($goods["price"]){
			$goods["goods_price"] = $goods["price"];
		}
		# 成本价
		if($goods["cost_price2"]){
			$goods["cost_price"] = $goods["cost_price2"];
		}
		# 库存
		if($goods["store_count"]){
			$goods["goods_number"] = $goods["store_count"];
		}
		# 冻结数量
		if($goods["store_frozen"]){
			$goods["frozen_number"] = $goods["store_frozen"];
		}
		# 转为二维数组返回
		$goods->toArray();
		return $goods;
	}
	# 查询添加到购物里面的所有商品
	public static function cartAll(){
		# 判断是否登录
		if(session("?user_info")){
			# 登录了，查询数据
			$user_id = session("user_info.id");
			$data = \app\common\model\Cart::where("user_id",$user_id)->field("id,user_id,goods_id,spec_goods_id,number,is_selected")->select();
			$data = (new \think\Collection($data))->toArray();
		}else{
			# 没有登录查询cookie
			$data = cookie("cart")?:[];
			$data = array_values($data);
		}
		return $data;
	}
	# 删除购物车数据
	public static function delcart($id){
		if(session("?user_info")){
			# 已登录从数据表中删除
			$user_id = session("user_info.id");
			$find = \app\common\model\Cart::where(["id"=>$id,"user_id" => $user_id])->delete();
		}else{
			# 未登录 删除cookie中数据
			$data = cookie("cart")?:[];
			unset($data[$id]);
			# 重新保存
			cookie("cart",$data,86400*7);
		}
	}
	# 修改商品数量
	public static function updataNumber($id,$number){
		if(session("?user_info")){
			$user_id = session("user_info.id");
			$find = \app\common\model\Cart::update(["number" => $number],["id" => $id,"user_id" => $user_id],true);
			// $find->number = $number;
			// $find->save();/
		}else{
			$data = cookie("cart")?:[];
			$data[$id]["number"] = $number;
			cookie("cart",$data,86400*7);
		}
	}
	# 修改选中状态
	public static function changeStatus($id,$is_selected){
		if(session("?user_info")){
			$user_id = session("user_info.id");
			$where["user_id"] = $user_id;
			if($id !== "all"){
				$where["id"] = $id;
			}
			\app\common\model\Cart::update(["is_selected" => $is_selected],$where,true);
		}else{
			$data = cookie("cart")?:[];
			if($id != "all"){
				$data[$id]["is_selected"] = $is_selected;
			}else{
				foreach ($data as $key => $value) {
					$data[$key]["is_selected"] = $is_selected;
				}
			}
			cookie("cart",$data,86400*7);
		}
	}
	# 修改登录将cookie中的数据传到数据库中
	public static function cookieSql(){
		$data = cookie("cart")?:[];
		foreach ($data as $key => $value) {
			self::getAddCart($value["goods_id"],$value["spec_goods_id"],$value['number']);
		}
		cookie("cart",null);
	}
}